package com.sonny.jdom;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.XSLTransformer;
import org.jdom.xpath.XPath;

public class JdomTest {

	/**
	 * @param args
	 */

	final static String xmlTestFile1 = "car.xml";
	final static String xslTestFile1 = "car.xsl";
	final static String xmlTestFile2 = "car2.xml";

	public static void main(String[] args) throws IOException, JDOMException {

		ReadXMLDoc();

		CreateNewXMLDoc();

		XSLTTest();

		XPATHTest();
		
		System.out.println("Completed");

	}

	public static void ReadXMLDoc() throws IOException {

		try {

			SAXBuilder saxbuilder = new SAXBuilder();
			
			Document myDocument = saxbuilder.build(new File(xmlTestFile1));

			XMLOutputter out = new XMLOutputter(
					org.jdom.output.Format.getPrettyFormat());
			StringWriter writer = new StringWriter();
			out.output(myDocument, writer);
			System.out.println(writer.toString());

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}

	}

	public static void CreateNewXMLDoc() throws IOException {

		// Create the root element
		Element carElement = new Element("car");
		// create the document
		Document myDocument = new Document(carElement);
		// add an attribute to the root element
		carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90"));
		// add a comment
		carElement.addContent(new Comment("Description of a car"));

		// add some child elements
		Element make = new Element("make");
		make.addContent("Toyota");
		carElement.addContent(make);

		// add some more elements
		carElement.addContent(new Element("model").addContent("Celica"));
		carElement.addContent(new Element("year").addContent("1997"));
		carElement.addContent(new Element("color").addContent("green"));
		carElement.addContent(new Element("license").addContent("1ABC234")
				.setAttribute("state", "CA"));

		try {
			// get element
			Element yearElement = carElement.getChild("year");
			System.out.println("Year:" + yearElement.getTextTrim());

			// remove element
			boolean removed = carElement.removeChild("year");

			// add new element
			carElement.addContent(new Element("year").addContent("2009"));
			carElement.addContent(new Element("year").addContent("2010")); // IGNORED
			carElement.addContent(new Element("year").addContent("2011")); // IGNORED

			yearElement = carElement.getChild("year");
			System.out.println("Year: (ajusted)" + yearElement.getTextTrim());

		} catch (NullPointerException e) {
			System.err.println("Year:"
					+ "NULL pioint is returned because no more 'YEAR' element");
			e.printStackTrace();
		}

		// write to XML file
		XMLOutputter outputter = new XMLOutputter();
		outputter.output(myDocument, System.out);

		/*
		 * Output to file FileWriter writer = new FileWriter(xmlTestFile);
		 * outputter.output(myDocument, writer); writer.close();
		 */
	};

	public static void XSLTTest() throws JDOMException, IOException {

		// READ XML
		SAXBuilder saxbuilder = new SAXBuilder();
		Document myDocument = saxbuilder.build(new File(xmlTestFile1));

		// READ XSL
		XSLTransformer transformer = new XSLTransformer(xslTestFile1);

		// TRANSFORMATION
		Document resultDoc = transformer.transform(myDocument);

		XMLOutputter out = new XMLOutputter();
		Format prettyFormat = org.jdom.output.Format.getPrettyFormat();
		out.setFormat(prettyFormat);

		StringWriter writer = new StringWriter();
		out.output(resultDoc, writer);
		System.out.println(writer.toString());

	}

	public static void XSLTTest2() throws FileNotFoundException, Exception {

		TransformerFactory tFactory = TransformerFactory.newInstance();

		SAXBuilder saxbuilder = new SAXBuilder();
		Document myDocument = saxbuilder.build(new File(xmlTestFile1));

		// Make the input sources for the XML and XSLT documents
		org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter();
		org.w3c.dom.Document domDocument = outputter.output(myDocument);
		javax.xml.transform.Source xmlSource = new javax.xml.transform.dom.DOMSource(
				domDocument);

		// Get a XSLT transformer
		StreamSource xsltSource = new StreamSource(new FileInputStream(
				xslTestFile1));
		Transformer transformer = tFactory.newTransformer(xsltSource);

		// Do the transform
		transformer.transform(xmlSource, null);

	}

	public static void XPATHTest() throws JDOMException, IOException {
		// READ XML
		SAXBuilder saxbuilder = new SAXBuilder();
		Document myDocument = saxbuilder.build(new File(xmlTestFile1));

		XPath xpath = XPath.newInstance("/collection/dvd"); // XPath 
		List list = xpath.selectNodes(myDocument); // Document 

	}

}
